/**
 * Created by Delicate on 2017/5/31.
 */
$(document).ready(function(){
    init();
    monitor();
    service();
});
    
function init(){
    load(1);
    $("#jumpVal").keydown(function(e) {  
	        if (e.keyCode == 13) {  
	              jump();
	        }  
	   }); 
   }
function monitor(){
    $(".u-select-option").on("click", function () {
        if (!$(this).hasClass("u-select-option-active")) {
            $(".u-select-option-active").removeClass("u-select-option-active");
            $(this).addClass("u-select-option-active");
            load(1);
        }
    });
    $(".task-search-btn").on("click",function(res){
        load(1);
    })
    
}
function service(){

}

function load(number){
    $.ajax({
        type:"post",
        url:"/pay/orderList",
        datatype:"json",
        data:{
            pageNum: number,
            pageSize: 4
        },
        success:function(data){
            var _html = "";
            if(data.list.length>0){
                for (var i=0;i<data.list.length;i++){
                    var item = data.list[i];
                    _html += '<tr> ' +
                        '<td style="padding-left:10px">'+ item.order_no +'</td> ' +
                        '<td>'+item.order_desc+'</td> ' +
                        '<td style="color: #f97a5b;">'+item.total_fee+'</td> ' +
                        '<td >'+item.trade_type+'</td> ' +
                        '<td >'+item.create_time+'</td> ' +
                        '<td >'+item.status_str+'</td> ' +
                        '<td style="text-align:right;padding-right:50px;"> ';
                    	var cancle = "cancleOrder(this)"
                    	var continuPay = "continuPay(this)"
                        if (item.status==0){
                            _html+="<div data-id='"+item.id+"' data-orderno='"+item.order_no+"' class='table-link' style='border-radius:3px;background-color:#62bcc0'  onclick='"+continuPay+"'>继续微信支付</div>";
                        	_html+="<div data-id='"+item.id+"' data-orderno='"+item.order_no+"' class='table-link' style='border-radius:3px;margin-left: 5px;'  onclick='"+cancle+"'>取消</div>";
                        }
                        '</td> ' +
                        '</tr>';
                }
                page(data.totalPage,data.pageNumber);
                if(data.totalPage>1){
                	$("#m_choice_page_container").show();
                }else{
                	$("#m_choice_page_container").hide();
                }
            }else{
            	_html="<tr><td colspan='7' style='text-align:center;font-weight:500;padding-top:27px;position:initial;'>当前没有任何订单信息</td>"
            	$("#m_choice_page_container").hide();
            }
            $("#tbody").html(_html);
        }
    })
}
var totalPage=1;
function page(total, index){
	totalPage =total;
    laypage({
        cont: 'm_choice_page', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
        pages: total||1, //通过后台拿到的总页数
        curr: index || 1, //当前页
        jump: function (obj, first) { //触发分页后的回调
            if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
                load(obj.curr);
            }
        }
    });
}

function jump(){
	var jumpVal=$("#jumpVal").val();
	var re = /^[0-9]+.?[0-9]*$/;
	if (!re.test(jumpVal)) {
		layer.alert("请输入正确的整数");
        return;
	}else if(jumpVal<=0){
		layer.alert("请输入大于1的整数");
        return;
	}else if(jumpVal>totalPage){
		layer.alert("已经超出最大页码数了");
        return;
	}else if(jumpVal&&jumpVal<=totalPage){
		load(jumpVal);
	}
}


var ref="";
function queryOrderStatus(id){
	
	$.ajax({
        type:"POST",
        url:"/pay/queryOrderStatus",
        data:{"id":id},
        datatype: "JSON",
        success:function(data){
        	if(data.result){
        		$("#result").html(data.msg+",3秒后为你刷新页面");
        		clearInterval(ref);
        		window.setTimeout("window.location.reload()",3000);
        		return;
        	}else{
        		      
        	}
        	
        },
	});
}



function cancleOrder(obj){
	var id = obj.getAttribute("data-id");
	var orderNo = obj.getAttribute("data-orderno");
	layer.confirm("确定取消当前订单么？", {
        btn: ['确定取消','再考虑一下'] //按钮
    }, function(){
    	layer.closeAll('dialog');
    	$.ajax({
            type:"POST",
            url:"/pay/cancleOrder",
            data:{"id":id,"order_no":orderNo},
            datatype: "JSON",
            success:function(data){
            	if(data.result){
            		layer.confirm(data.msg, {
                        btn: ['确定'] //按钮
                    }, function(){
                    	location.reload();
                    });
            	}else{
            		layer.alert(data.msg);        
            	}
            	
            },
		});
    },function(){
    	
    });
}



function continuPay(obj){
	var id = obj.getAttribute("data-id");
	var orderNo = obj.getAttribute("data-orderno");
	
	layer.confirm("确定继续支付当前订单么？", {
        btn: ['生成付款码','再考虑一下'] //按钮
    }, function(){
    	layer.closeAll('dialog');
    	
    	$.ajax({
            type:"POST",
            url:"/pay/continuPay",
            data:{"id":id,"order_no":orderNo},
            datatype: "JSON",
            success:function(data){
            	if(data.result){
            		$("#payCodeTrigger").trigger("click");
	            	$('#qrcode').html("");
	            	$('#qrcode').qrcode({
	            		width: 150,
	            		height: 150,
	            		text: data.msg
	            	});
	            	ref = window.setInterval("queryOrderStatus("+id+")",1000); 
            	}else{
            		layer.alert(data.msg);        
            	}
            	
            },
		});
    },function(){
    	
    });
}

